package com.nuoniu.sibanyun.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.SysRole;
import com.nuoniu.sibanyun.entity.dto.SysRoleDto;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-04-23
 */
@Mapper
@Component
public interface SysRoleMapper extends BaseMapper<SysRole> {
    /**
     * 常用 接口 需要 优化（）
     * @param userId
     * @return
     */
    @Select("SELECT r.*  FROM sys_role r INNER JOIN sys_user_role ur ON ur.role_id = r.id AND ur.user_id = #{userId} AND r.`status` = 0 ")
    List<SysRole> listByUserId(@Param("userId") Long userId);

    @Select("SELECT r.*  FROM sys_role r INNER JOIN sys_role_menu rm ON rm.role_id = r.id AND rm.menu_id = #{menuId} AND r.`status` = 0 ")
    List<SysRole> listByMenuId(@Param("menuId") Long menuId);

    @Insert("INSERT INTO sys_role (role_name, remarks, role_sort, data_scope, create_by, company_id, company_name) VALUES (#{roleName}, #{remarks}, #{roleSort}, #{dataScope}, #{createBy}, #{companyId}, #{companyName})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer insertRole(SysRole sysRole);

    @Select("select id as 'key', id, role_name roleName, remarks from sys_role where company_id = #{companyId} ")
    List<SysRoleDto> getRoleAll(Integer companyId);
}
